package com.hua.argi;

/**
 * @author: _hua
 * @created: 2019/3/26.
 * @updater:
 * @description:
 */
public class MyQueue {

    private Object queArray[];
    private int front ;
    private int rear;
    private int nItems;
    private int maxSize;

    public MyQueue(int s){
        maxSize = s;
        queArray = new Object[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;

    }
    //新增
    public void insert(int value){
        if(isFull()){
            System.out.println("队列已满==================");
        }else{
            //如果队列尾部指向顶了，那么循环回来，执行队列的第一个元素
            if(rear==maxSize-1){
                rear=-1;
            }
            //队尾指针加1，然后在队尾指针处插入新的数据
            queArray[++rear]=value;
            nItems++;
        }

    }
    //移除
    public Object remove(){
        Object removeValue = null ;
        if(!isEmpty()){
            removeValue=queArray[front];
            queArray[front] = null;
            front++;
            nItems--;
            if(front==maxSize){
                front=0;
            }
        }
        return removeValue;
    }

    //是否为空
    public boolean isEmpty(){
        return (nItems==0);
    }

    //是否满
    public boolean isFull(){
        return (nItems==maxSize);
    }

    //首元素查看
    public Object peek(){
        return queArray[front];
    }

    public int getSize(){
        return nItems;
    }
}
